#!/usr/bin/env python

import sys
import optparse
import pprint
import allconsuming.filedac

def parseArgs(args):
    optParser = optparse.OptionParser()
    optParser.add_option('--tag', action='store', dest='tag', type='string', default=None,
                         help='only display items with specified tag')
    
    return optParser.parse_args(args)

def main(args=None):
    if args == None:
        args = sys.argv[1:]

    (options, args) = parseArgs(args)
    print args, options
    userName = args[0]

    allTags = {}
    consumables = allconsuming.filedac.getUserConsumables(userName)
    for consumable in consumables:
        if options.tag is None or options.tag in consumable.tags:
            for tag in consumable.tags:
                allTags[tag] = allTags.get(tag, 0) + 1

    tags = allTags.items()
    tags = [(-t[1], t) for t in tags]
    tags.sort()
    tags = [t[1] for t in tags]
    for (tag, count) in tags:
        print tag + ': ' + str(count)
    
    return 0


if __name__ == '__main__':
    sys.exit(main())

